﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace HeliGame
{
    public class Camera
    {
        public Vector3 position;
        public Vector3 lookat;
        public Vector3 up;

        Quaternion rotation = new Quaternion(0, 0, 0, 1);

        float standardDistance = 1;

        public void MoveBehindHeli(Vector3 heliPos, float heliYaw, float heliPitch, float heliRoll)
        {

            rotation = Quaternion.Lerp(rotation, Quaternion.CreateFromYawPitchRoll(-heliYaw, -heliPitch * 0.6f, -heliRoll * 0.2f), 0.06f);

            position = heliPos + Vector3.Transform(new Vector3(-0.3f, 2.5f - heliPos.Y * 0.5f, 4), rotation);
            
            lookat = heliPos + new Vector3(0, 0.0f, 0);
            up = Vector3.Transform(Vector3.UnitY, rotation * 2);
            standardDistance = (position - heliPos).Length();
        }

        public void Clamp(Vector3 camCollisionPoint, float ceilingHeight)
        {
            position = camCollisionPoint; // +(lookat - camCollisionPoint) * 0.001f;

            if (position.Y < 0) position.Y = 0;
            if (position.Y > ceilingHeight) position.Y = ceilingHeight;

            float distance = (position - lookat).Length();
            if (distance < standardDistance)
            {
                float offsetY = standardDistance - distance;
                lookat = lookat + new Vector3(0, 0.1f * offsetY , 0);
            }
        }
    }
}
